package ms.platform.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class CookiesUtil  {
    /**
     * 设置cookie
     *
     * @param response
     * @param key cookie名字
     * @param value cookie值
     * @param maxAge cookie生命周期 以秒为单位
     * @param path cookie传递路径
     * @param domain cookie域
     */
    public static void addCookie(HttpServletResponse response,
            String key, String value, int maxAge, String path, String domain)
    {
        Cookie cookie = new Cookie(key, value);
        cookie.setPath(path);
        cookie.setDomain(domain);
        if (maxAge > 0)
        {
            cookie.setMaxAge(maxAge);
        }
        response.addCookie(cookie);
    }
    
    
  

    /**
     * 根据名字获取cookie
     *
     * @param request
     * @param name cookie名字
     * @return
     */
    public static Cookie getCookieByName(HttpServletRequest request, String name)
    {
        Cookie cookies[] = request.getCookies();
        if (cookies != null)
        {
            for (int i = 0; i < cookies.length; i++)
            {
                Cookie cookie = cookies[i];
                if (name.equals(cookie.getName()))
                {
                    return cookie;
                }
            }
        }
        return null;
    }
}
